package org.example.springmvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Enumeration;

/**
 * ClassName: Hello05Controller
 * Package: org.example.springmvc.controller
 * Description:
 *
 * @Author wanghao
 * @Create 2024-07-11 16:47
 * @Version 1.0
 */
@Controller
@RequestMapping("/hello05")
public class Hello05Controller {

    // 演示获取请求头信息
    @RequestMapping("/h01")
    public String h01(@RequestHeader(name = "Accept") String accept, HttpServletRequest request) {
        System.out.println("accept = " + accept);
        System.out.println("==================================");
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String headName = headerNames.nextElement();
            String headerValue = request.getHeader(headName);
            System.out.println(headName + " : " + headerValue);
        }
        return "succ";
    }

    // 演示获取cookie的值
    // HttpSession session  参数不能省略，否则服务器不会返回给客户端sessionid
    @RequestMapping("/h02")
    public String h02(@CookieValue(value = "JSESSIONID", defaultValue = "missing") String jsessionid, HttpServletRequest request, HttpSession session) {

        System.out.println("jsessionid = " + jsessionid);
        System.out.println("==================================");
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            System.out.println(cookie.getName() + " : " + cookie.getValue());
        }
        return "succ";
    }
}
